package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //1.新增dish
        dishMapper.insert(dish); //id已经回填了
        //2.新增dish_flavor
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
        }
        //进行批量插入
        dishFlavorMapper.insertBatch(flavors);


    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> dishVOS =  dishMapper.page(dishPageQueryDTO);

        return new PageResult(dishVOS.getTotal(),dishVOS.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishs(List<Long> ids) {
        //1.判断是不是起售的菜品
        ids.forEach(id -> {
             Dish dish =    dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //2.判断是不是被套餐关联的菜品
        Integer num = setmealDishMapper.countByIds(ids);
        if (num > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        }

        //3.删除菜品后，关联的口味数据也要删除掉
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByIds(ids);

    }

    @Override
    public void enableOrDisable(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
    }

    @Override
    public DishVO selectDishById(Long id) {
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> flavorByDishId = dishFlavorMapper.getFlavorByDishId(id);

        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish, dishVO);
        BeanUtils.copyProperties(flavorByDishId, dishVO);
        return dishVO;

    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //更新菜品前要将dishFlavor删除干净

        dishFlavorMapper.deleteByDishId(dish.getId());

       //更新dish表
        dishMapper.update(dish);
        //更新dishflavor表
        List<DishFlavor> flavors = dishDTO.getFlavors();//当前没有dishid
        //补充dishId
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
        }
        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public List<Dish> list(Long categoryId) {

        Dish dish = Dish.builder().id(categoryId).build();
        return  dishMapper.selectByCategoryId(dish);

    }


    @Override
    public List<DishVO> listAll(Long categoryId) {

       List<DishVO> dishVOS = new ArrayList<>();

        Dish dishfind = Dish.builder().status(StatusConstant.ENABLE).categoryId(categoryId).build();
        List<Dish> dishes = dishMapper.selectByCategoryId(dishfind);
        dishes.forEach(dish -> {
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorByDishId(dish.getId());
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);

            /*BeanUtils.copyProperties(flavors, dishVO.getFlavors());*/
            //不能使用BeanUtils进行属性的拷贝
            dishVO.setFlavors(flavors);
            dishVOS.add(dishVO);
        });
        return dishVOS;


    }
}
